define([
    "dojo/_base/declare",
    "gistech/widgets/normalDialog/NormalDialog",
    "dojo/text!./templates/Template.html",
    "dojo/_base/lang",
    "dojo/dom-style",
    "dojo/dom-construct",
    "dojo/on","dojo/topic",
    "dojo/_base/connect",
    "gistech/util/ajaxUtil",
    "esri/geometry/geodesicUtils",
    "esri/geometry/webMercatorUtils",
    "esri/layers/FeatureLayer","esri/InfoTemplate",
    "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol",
    "esri/renderers/UniqueValueRenderer", "esri/Color",
    "dojo/dom-style","dojo/query","dojo/dom-class"],
    function(declare,NormalDialog,template,lang,style,domConstruct,
    		on,topic,eventconnect,ajaxUtil,geodesicUtils,webMercatorUtils,FeatureLayer,
    		InfoTemplate,SimpleLineSymbol,SimpleFillSymbol,UniqueValueRenderer,Color,
    		domStyle,query,domClass){
        return declare([NormalDialog],{
        	
            templateString:template,

            projectType:"核查项目",
            
            LandFeatureLayer:null,
            landinfoTemplate:null,
            
            allowGet:null,
            constructor:function (args) {
            	this.inherited(arguments);
            	this.map = args.map;
            	this.landgeo = args.landgeo;
            },
            postCreate:function(){
                this.inherited(arguments);
                on(this.save, "click",lang.hitch(this,this.saveLandGeometryToDB));
                on(this.getLandInfo,"click",lang.hitch(this,this.allowGetLandInfo));
            },
            startup:function(){
                this.inherited(arguments);
               
                this.setLandGeometry(this.landgeo);
                this.init();
            },
            init:function(){
            	this.allowGet = false;
            	this.addLandsFeatureLayer();
            	 dojo.query("#XH")[0].value = "L"+getDateStr();
            	 dojo.query("#REGISTERDATE")[0].value = reduceformatDate(0,"-");
            	 var projecttype = dojo.query(".projecttype input");
                 for(var i=0;i<projecttype.length;i++){
     				on(projecttype[i], "click",lang.hitch(this,this.changeProjectTypeHandler));
     			}
                 topic.subscribe("MapLandClickHandler",lang.hitch(this,this.listenerMapLandClickHandler));
            },
            allowGetLandInfo:function(evt){
            	var item = evt.currentTarget;
            	this.allowGet = item.checked;
            	style.set(dojo.byId("alertinfo"),"display",this.allowGet?"block":"none");
            },
            addLandsFeatureLayer:function (){
            	
            	var linesymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
            			new Color([0,0,0,0.5]), 0.2);
            	
            	var symbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
    					linesymbol,new Color([150, 150, 150, 0.35]));
            	
            	var renderer = new UniqueValueRenderer(symbol, "EXPENDTYPE");
            	renderer.addValue("旧厂改造",// new SimpleFillSymbol().setColor(new Color([56, 168, 0, 0.5])));
            			new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            					linesymbol,new Color([56, 168, 0, 0.35])
            			));
            	renderer.addValue("村留地项目", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([139, 209, 0, 0.35])));
            	renderer.addValue("股权收购", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([255, 255, 0, 0.35])));
            	renderer.addValue("旧村改造", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([255, 128, 0, 0.35])));
            	renderer.addValue("旧城改造", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([255, 0, 0, 0.35])));
            	renderer.addValue("经济发展用地", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([127, 127, 127, 0.35])));
            	renderer.addValue("招拍挂", new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
            			linesymbol,new Color([0, 255, 0, 0.35])));
            	this.LandFeatureLayer = new FeatureLayer(mapinfoTemplate["LandFeatureLayer"].layerurl, {
            		id:"LandFeatureLayer",
            		mode: FeatureLayer.MODE_SNAPSHOT,
            		outFields: ["*"],
            		infoTemplate: new InfoTemplate(mapinfoTemplate["LandFeatureLayer"].template)
            	});

            	this.LandFeatureLayer .setRenderer(renderer);
            	this.map.addLayer(this.LandFeatureLayer ,0);



            	on(this.LandFeatureLayer ,"click", lang.hitch(this,function(evt) {
            		topic.publish("MapLandClickHandler",evt);
            		
            	}));
            },
            listenerMapLandClickHandler:function(evt){
            	var layer = evt.currentTarget;
            	var layerid = layer.id.split("_")[0];//"LandFeatureLayer_layer"
            	if(this.allowGet){
            		var attrs = evt.graphic.attributes;

            		var fieldmap = mapinfoTemplate[layerid].fieldmap;
            		if(fieldmap.hasOwnProperty('LANDBH')){
            			dojo.query("#LANDBH")[0].value = attrs[fieldmap["LANDBH"]];     
            		}
            		if(fieldmap.hasOwnProperty('CITY')){ 
            			dojo.query("#CITY")[0].value = attrs[fieldmap["CITY"]]; 
            		}
            		if(fieldmap.hasOwnProperty('DISTRICT')){ 
            			dojo.query("#DISTRICT")[0].value = attrs[fieldmap["DISTRICT"]];  
            		}
            		if(fieldmap.hasOwnProperty('ZHENJIE')){ 
            			dojo.query("#ZHENJIE")[0].value = attrs[fieldmap["ZHENJIE"]]; 
            		}
            		if(fieldmap.hasOwnProperty('ADDRESS')){ 
            			dojo.query("#ADDRESS")[0].value = attrs[fieldmap["ADDRESS"]];  
            		}
            		if(fieldmap.hasOwnProperty('PROJECTTYPE')){ 
            			this.getIndexFromSelectListByVaue("PROJECTTYPE",attrs[fieldmap["PROJECTTYPE"]]);
            		}
            		if(fieldmap.hasOwnProperty('EXPENDTYPE')){ 
            			this.getIndexFromSelectListByVaue("EXPENDTYPE",attrs[fieldmap["EXPENDTYPE"]]);
            		}
            		if(fieldmap.hasOwnProperty('TGXX')){ 
            			this.getIndexFromSelectListByVaue("TGXX",attrs[fieldmap["TGXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('KGXX')){ 
            			this.getIndexFromSelectListByVaue("KGXX",attrs[fieldmap["KGXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('XZJSXX')){ 
            			this.getIndexFromSelectListByVaue("XZJSXX",attrs[fieldmap["XZJSXX"]]);
            		}
            		if(fieldmap.hasOwnProperty('VOLUMERATIO')){ 
            			dojo.query("#VOLUMERATIO")[0].value = attrs[fieldmap["VOLUMERATIO"]];  
            		}
            		if(fieldmap.hasOwnProperty('OWNERSHIP')){ 
            			this.getIndexFromSelectListByVaue("OWNERSHIP",attrs[fieldmap["OWNERSHIP"]]);
            		}
            		if(fieldmap.hasOwnProperty('COMPANY')){ 
            			dojo.query("#COMPANY")[0].value =  attrs[fieldmap["COMPANY"]];     
            		}
            		if(fieldmap.hasOwnProperty('LICENCE')){ 
            			dojo.query("#LICENCE")[0].value = attrs[fieldmap["LICENCE"]];       
            		}
            		if(fieldmap.hasOwnProperty('QSR')){ 
            			this.getIndexFromSelectListByVaue("QSR",attrs[fieldmap["QSR"]]);
            		}
            		if(fieldmap.hasOwnProperty('QSRNAME')){ 
            			dojo.query("#QSRNAME")[0].value = attrs[fieldmap["QSRNAME"]];       
            		}
            		if(fieldmap.hasOwnProperty('TELL')){ 
            			dojo.query("#TELL")[0].value = attrs[fieldmap["TELL"]];      
            		}
            		if(fieldmap.hasOwnProperty('BZJ')){ 
            			dojo.query("#BZJ")[0].value = attrs[fieldmap["BZJ"]];   
            		}
            		if(fieldmap.hasOwnProperty('LANDVALUE')){ 
            			dojo.query("#LANDVALUE")[0].value = attrs[fieldmap["LANDVALUE"]];    
            		}
            		if(fieldmap.hasOwnProperty('YJQYSJ')){ 
            			dojo.query("#YJQYSJ")[0].value = attrs[fieldmap["YJQYSJ"]];  
            		}
            		if(fieldmap.hasOwnProperty('SJQYSJ')){ 
            			dojo.query("#SJQYSJ")[0].value = attrs[fieldmap["#SJQYSJ"]]; 
            		}
            		if(fieldmap.hasOwnProperty('BZ')){ 
            			dojo.query("#BZ")[0].value = attrs[fieldmap["BZ"]] ;
            		}
            	}else{
            		return;
            	}
            },
            getIndexFromSelectListByVaue:function(selectId,checkValue){
            	var select = dojo.byId(selectId);  
                for(var i=0; i<select.options.length; i++){  
                    if(select.options[i].innerHTML == checkValue){  
                        select.options[i].selected = true;  
                        break;  
                    }  
                } 
                return;
            },
            closeDialog:function(){
            	topic.publish("LandExpandManageWidgetDestroy");
            	
    			this.inherited("closeDialog",arguments);
    			this.destroy();
    		},

    		changeProjectTypeHandler:function(item){
    			var selectitem = item.currentTarget;
    			this.projectType = selectitem.attributes["value"].value;
    			var isshow = false;
    			if(this.projectType=="核查项目"){
    				isshow = true;
    			}else if(this.projectType=="洽谈项目"){
    				isshow = false;
    				dojo.byId("YJQYSJ_DESC").innerHTML = "预计签约时间：";
    				dojo.byId("SJQYSJ_DESC").innerHTML = "预计获取时间：";
    			}else{
    				isshow = false;
    				dojo.byId("YJQYSJ_DESC").innerHTML = "实际签约时间：";
    				dojo.byId("SJQYSJ_DESC").innerHTML = "实际获取时间：";
    			}
    			dojo.toggleClass(dojo.byId("QYSJ"),"unshowed",isshow);
    		},
            refreshLandGeometry:function(){
            	var geo = webMercatorUtils.webMercatorToGeographic(this.landgeo.geometry);
            	var area=Math.abs(geodesicUtils.geodesicAreas([geo], esri.Units.SQUARE_METERS));
    			dojo.byId("TOTALAREA").value = area.toFixed(3);
    			dojo.byId("NETLANDAREA").value = area.toFixed(3);
            },
            setLandGeometry:function(graphic){
            	this.landgeo = graphic;
            	var geo = webMercatorUtils.webMercatorToGeographic(graphic.geometry);
            	var area=Math.abs(geodesicUtils.geodesicAreas([geo], esri.Units.SQUARE_METERS));
    			dojo.byId("TOTALAREA").value = area.toFixed(3);
    			dojo.byId("NETLANDAREA").value = area.toFixed(3);
            },
            saveLandGeometryToDB:function(){
				this.landgeo.attributes={
						XH:dojo.query("#XH")[0].value,           
						TYPE:this.projectType,         
						LANDBH:dojo.query("#LANDBH")[0].value,       
						LANDNAME:dojo.query("#LANDNAME")[0].value,     
						CITY:dojo.query("#CITY")[0].value,         
						DISTRICT:dojo.query("#DISTRICT")[0].value,     
						ZHENJIE:dojo.query("#ZHENJIE")[0].value,      
						ADDRESS:dojo.query("#ADDRESS")[0].value,      
						PROJECTTYPE:dojo.query("#PROJECTTYPE")[0].value,  
						EXPENDTYPE:dojo.query("#EXPENDTYPE")[0].value,   
						TGXX:dojo.query("#TGXX")[0].value,         
						KGXX:dojo.query("#KGXX")[0].value,         
						XZJSXX:dojo.query("#XZJSXX")[0].value,       
						TOTALAREA:dojo.query("#TOTALAREA")[0].value,    
						NETLANDAREA:dojo.query("#NETLANDAREA")[0].value,  
						VOLUMERATIO:dojo.query("#VOLUMERATIO")[0].value,  
						OWNERSHIP:dojo.query("#OWNERSHIP")[0].value,    
						COMPANY:dojo.query("#COMPANY")[0].value,      
						LICENCE:dojo.query("#LICENCE")[0].value,      
						QSR:dojo.query("#QSR")[0].value,          
						QSRNAME:dojo.query("#QSRNAME")[0].value,      
						TELL:dojo.query("#TELL")[0].value,         
						BZJ:dojo.query("#BZJ")[0].value,          
						LANDVALUE:dojo.query("#LANDVALUE")[0].value,    
						REGISTERDATE:dojo.query("#REGISTERDATE")[0].value, 
						REGISTERUSER:loginusername.loginname, 
						YJQYSJ:dojo.query("#YJQYSJ")[0].value,       
						SJQYSJ:dojo.query("#SJQYSJ")[0].value,       
						BZ:dojo.query("#BZ")[0].value 
				};
				this.LandFeatureLayer.applyEdits([this.landgeo],null,null,function(evt){hideLoading();alert("保存成功！")},function(evt){hideLoading();alert("保存失败！")});
				showLoading();
            },
    });
});

